package com.tcpengine.dao;

import java.io.UnsupportedEncodingException;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;


public class UserInfo {
	private String strName;	// 字符串
	private Integer userId;		// 32位整型
	private Float money;		// 浮点
	private Long gold;		// 64位整型
	private Byte sex;		// 一个字节
	private Short age;		// 两个字节
	public String getStrName() {
		return strName;
	}
	public void setStrName(String strName) {
		this.strName = strName;
	}
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public Float getMoney() {
		return money;
	}
	public void setMoney(Float money) {
		this.money = money;
	}
	public Long getGold() {
		return gold;
	}
	public void setGold(Long gold) {
		this.gold = gold;
	}
	public Byte getSex() {
		return sex;
	}
	public void setSex(Byte sex) {
		this.sex = sex;
	}
	public Short getAge() {
		return age;
	}
	public void setAge(Short age) {
		this.age = age;
	}
	
	public UserInfo(String strName, Integer userId, Float money, Long gold, Byte sex, Short age) {
		this.strName = strName;
		this.userId = userId;
		this.money = money;
		this.gold = gold;
		this.sex = sex;
		this.age = age;
	}
	
	public int sizeof() throws UnsupportedEncodingException {
		Integer iLength = 0;
		Integer iNameLen = strName.getBytes("UTF-8").length;
		iLength += Integer.SIZE / 8;
		iLength += iNameLen;
		iLength += Integer.SIZE / 8;
		iLength += Float.SIZE / 8;
		iLength += Long.SIZE / 8;
		iLength += Byte.SIZE / 8;
		iLength += Short.SIZE / 8;
		return iLength;
	}

	// 系统化到内存列表
	public ByteBuf toByteBuf() throws UnsupportedEncodingException {
		ByteBuf byteBuf = Unpooled.buffer(sizeof());
		byteBuf.writeInt(strName.getBytes("UTF-8").length);
		strName.getBytes("UTF-8");
		byteBuf.writeBytes(strName.getBytes("UTF-8"));
		byteBuf.writeInt(userId);
		byteBuf.writeFloat(money);
		byteBuf.writeLong(gold);
		byteBuf.writeByte(sex);
		byteBuf.writeShort(age);
		return byteBuf;
	}

}
